package week4th.jsjf;

import week4th.jsjf.exceptions.ElementNotFoundException;

public class ArrayUnorderedList<T> extends ArrayList<T>
         implements UnorderedListADT<T>
{

    public ArrayUnorderedList()
    {
        super();
    }

    public ArrayUnorderedList(int initialCapacity)
    {
        super(initialCapacity);
    }

    public void addToFront(T element)
    {
        for (int i =0;i<size();i++){
            alist[i]=list[i+1];
        }
        for (int i =0;i<size();i++){
            list[i+1]=alist[i];
        }
        list[0]=element;
        rear++;
    }

    public void addToRear(T element)
    {
        if (list[0]==null){
            list[0]=element;
        }
        else {
            int i =0;
            while (list[i]!=null){
                i++;
            }
            list[i]=element;
        }
        rear++;
    }

    public void addAfter(T element, T target)
    {
        if (size() == list.length)
            expandCapacity();

        int scan = 0;

        while (scan < rear && !target.equals(list[scan])) 
            scan++;
      
        if (scan == rear)
            throw new ElementNotFoundException("UnorderedList");
    
        scan++;

        for (int shift=rear; shift > scan; shift--)
            list[shift] = list[shift-1];

		list[scan] = element;
        rear++;
		modCount++;
    }
}
